//
//  NotPubTableViewController.m
//  app001
//
//  Created by xionghao on 15/10/9.
//  Copyright © 2015年 xionghao. All rights reserved.
//

#import "NotPubTableViewController.h"
#import "ZTActivityNopubCell.h"
#import "Puser.h"
#import <MJRefresh.h>
#import "ZTActivityModel.h"
#import "ZTListContent.h"
#import "UserInfoUpdateResde.h"
#import "PostActivityTableViewController.h"
#import "MYActivityDetailViewController.h"

@interface NotPubTableViewController ()<BottomBarBtnClickDelegate,BottombarDataSource,UIAlertViewDelegate>
{
    BOOL _candelete;
}
@property (nonatomic,strong) NSMutableArray *datalist;
@property (nonatomic,strong) Puser *user;



@end

@implementation NotPubTableViewController
-(Puser *)user{
    if (_user==nil) {
        _user=[Puser getUser];
    }
    return _user;
}


-(void)setIsEidt:(BOOL)isEidt{
    _isEidt=isEidt;
    for (ZTActivityModel *mode in self.datalist) {
        mode.ischecked=NO;
}
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.backgroundColor=bgGarycolor;
    self.tableView.contentInset=UIEdgeInsetsMake(0, 0, 46, 0);
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight=88;
    [self setuptableView];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
-(void)setuptableView{
    MJRefreshNormalHeader *header=[MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    self.tableView.header=header;
    MJRefreshBackNormalFooter *footer=[MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadmoredata)];
    
    self.tableView.footer=footer;
    
    self.tableView.header=header;
    
    
   
    
}
-(void)loadmoredata{
    NSMutableDictionary *params=[NSMutableDictionary dictionary];
    if (self.isGet) {
        params[@"action"]=@"my_mark";
    }else{
        params[@"action"]=@"my_activity";
    }
    params[@"fabu"]=@"1";
    params[@"num"]=Kpagesize;
    NSInteger power=[self.user.power integerValue];
    
    params[@"user_id"]=power<5?self.user.user_id:self.user.com_id;
    params[@"type"]=power<5?@"person":@"company";
    NSMutableArray *IDarray=[NSMutableArray array];
    
    for (ZTActivityModel *mode in self.datalist) {
        [IDarray addObject:mode.ID];
    }
    
    params[@"page_id"]=[NSString DataTOjsonString:IDarray];
    
    
    [PostUitls Post:mynewzturl parameters:params success:^(id response) {
        
        NSDictionary *dict=response;
        [ZTActivityModel setupReplacedKeyFromPropertyName:^NSDictionary *{
            return @{@"ID":@"id"};
        }];
        [ZTActivityModel setupObjectClassInArray:^NSDictionary *{
            return @{@"content":@"ZTListContent"};
        }];
        
        NSArray *data=[ZTActivityModel objectArrayWithKeyValuesArray:dict[@"data"]];
        [self.datalist addObjectsFromArray:data];
        if (data.count<[Kpagesize integerValue]) {
            [self.tableView.footer noticeNoMoreData];
        }else{
            [self.tableView.footer resetNoMoreData];
        }
        [self.tableView reloadData];
        [self.tableView.footer endRefreshing];
        
        
        
    } failure:^(NSError *error) {
        [self.tableView.footer endRefreshing];
    }];
}

-(NSMutableArray *)datalist{
    if ( _datalist==nil) {
        _datalist=[NSMutableArray array];
    }
    return  _datalist;
}
-(NSInteger)numberofBottobarcount:(BottomBar *)bottombar{
    return self.datalist.count;
}
-(void)refresh{
    NSMutableDictionary *params=[NSMutableDictionary dictionary];
    if (self.isGet) {
        params[@"action"]=@"my_mark";
    }else{
        params[@"action"]=@"my_activity";
    }
    params[@"fabu"]=@"1";
    params[@"num"]=Kpagesize;
    NSInteger power=[self.user.power integerValue];
    params[@"user_id"]=power<5?self.user.user_id:self.user.com_id;
    params[@"type"]=power<5?@"person":@"company";
    
    [PostUitls Post:mynewzturl parameters:params success:^(id response) {
        [self.datalist removeAllObjects];
        NSDictionary *dict=response;
        [ZTActivityModel setupReplacedKeyFromPropertyName:^NSDictionary *{
            return @{@"ID":@"id"};
        }];
        [ZTActivityModel setupObjectClassInArray:^NSDictionary *{
            return @{@"content":@"ZTListContent"};
        }];
        [ZTListContent setupReplacedKeyFromPropertyName:^NSDictionary *{
            return @{@"ID":@"id"};
        }];
        
        NSArray *data=[ZTActivityModel objectArrayWithKeyValuesArray:dict[@"data"]];
        if (data.count<[Kpagesize integerValue]) {
            [self.tableView.footer noticeNoMoreData];
        }else{
            [self.tableView.footer resetNoMoreData];
        }
        [self.datalist addObjectsFromArray:data];
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        
        
        
    } failure:^(NSError *error) {
        [self.tableView.header endRefreshing];
    }];
    
}

-(void)BottomBarPostClick:(BottomBar *)bottombar{
    PostActivityTableViewController *VC=[PostActivityTableViewController viewcontroller];
    VC.isActivtiy=!self.isGet;
    [self.navigationController pushViewController:VC animated:YES];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.tableView.header beginRefreshing];
}

-(void)BottomBar:(BottomBar *)bottombar otherbtnClick:(NSInteger)index{
    if (index==0) {
        [self showalret];
    }else{
        [self deleteOrDown:NO];
    }

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==0) {
        return;
    }else{
        [self deleteOrDown:YES];
    }
}

-(void)deleteOrDown:(BOOL)isdelete{
    
    MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    hud.labelText=@"正在操作";
    
    hud.margin = 10.f;
    hud.removeFromSuperViewOnHide = YES;
    NSMutableDictionary *params=[NSMutableDictionary dictionary];
    
 
    if (isdelete) {
        if (self.isGet) {
            params[@"action"]=@"del_fabu";
        }else{
            params[@"action"]=@"del_act";
        }
        
    }else{
        if (self.isGet) {
            params[@"action"]=@"add_fabu";
            
        }else{
            params[@"action"]=@"add_acts";
        }
    }

    
    NSInteger power=[self.user.power integerValue];
    params[@"user_id"]=power<5?self.user.user_id:self.user.com_id;
    params[@"type"]=power<5?@"person":@"company";
    
    NSMutableArray *IDarray=[NSMutableArray array];
    
    for (ZTActivityModel *mode in self.datalist) {
        if (mode.ischecked) {
            [IDarray addObject:mode.ID];
            
        }
    }
    params[@"jnid"]=[NSString DataTOjsonString:IDarray];
    [PostUitls Post:mynewzturl parameters:params success:^(id response) {
        hud.mode = MBProgressHUDModeText;
        
        UserInfoUpdateResde *resdemode=[UserInfoUpdateResde resdeWithDict:response];
        if ([resdemode.state integerValue]==400) {
            [self.tableView.header beginRefreshing];
            self.bottombar.isAllselected=NO;
            self.bottombar.btnenable=NO;
            if ([_delegate respondsToSelector:@selector(UPCompleteWithviewcontroller:)]) {
                [_delegate UPCompleteWithviewcontroller:self];
            }
        }
        hud.labelText=resdemode.msg;
        [hud hide:YES afterDelay:0.5];
    } failure:^(NSError *error) {
        
        hud.mode = MBProgressHUDModeText;
        hud.labelText=NetWorkError;
        [hud hide:YES afterDelay:1];
    }];
    
    
}

-(void)needupdate{
    [self.tableView.header beginRefreshing];
}
#pragma mark - Table view data source
-(void)showalret{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"确定删除么?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return self.datalist.count;
}

-(void)BottomBarAllclick:(BottomBar *)bottombar{
    for (ZTActivityModel *mode in self.datalist) {
        mode.ischecked=bottombar.allselected;
    }
    [self.tableView reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZTActivityModel *mode=self.datalist[indexPath.row];
    
    ZTActivityNopubCell *cell=[ZTActivityNopubCell cellWithTableView:tableView];
    cell.mode=mode;
    cell.isEidt=self.isEidt;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ZTActivityModel *mode=self.datalist[indexPath.row];
    
    if (self.isEidt) {
        
        ZTActivityNopubCell *cell=[tableView cellForRowAtIndexPath:indexPath];
        mode.ischecked=!mode.ischecked;
        cell.mode=mode;
        for (ZTActivityModel *mode in self.datalist) {
            if (mode.ischecked) {
                _candelete=YES;
                break;
            }else{
                _candelete=NO;
            }
        }
        self.bottombar.btnenable=_candelete;
        
    }else{
        MYActivityDetailViewController *vc=[[MYActivityDetailViewController alloc]init];
        vc.mode=mode;
        vc.isActiviy=!self.isGet;
        vc.isFromdown=YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
